home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 7 / FM Towns Free Software Collection 7.iso / taropyon / zmodem / src / main.c < prev    next >
C/C++ Source or Header  |  1993-11-30  |  13KB  |  651 lines

  1. /*************************************************************************
  2. *    for sz or rz
  3. *************************************************************************/
  4.  
  5. #include    <stdio.h>
  6. #include    <stdlib.h>
  7. #include    <stdarg.h>
  8. #include    <ctype.h>
  9. #include    <string.h>
  10. #include    <time.h>
  11. #include    <setjmp.h>
  12. #ifdef    __HIGHC__
  13. #    include    <msdos.cf>
  14. #    include    <heap.cf>
  15. #endif
  16.  
  17. #include    <sidework.h>
  18. #include    <loader.h>
  19. #include    <egb.h>
  20. #include    <mos.h>
  21. #include    <snd.h>
  22.  
  23. #include    <cnslib.h>
  24. #include    <dialog.h>
  25. #include    <doscons.h>
  26. #include    <dta.h>
  27. #include    <dtaname.h>
  28. #include    <egbmacro.h>
  29. #include    <event.h>
  30. #include    <fslib.h>
  31. #include    <keycode.h>
  32. #include    <kyb.h>
  33. #include    <menu.h>
  34. #include    <moscur.h>
  35. #include    <sbar.h>
  36. #include    <setint.h>
  37. #include    <spicon.h>
  38.  
  39. #include    "usrlib.h"
  40. #include    "rsctrl.h"
  41. #include    "flib.h"
  42. #define    _MSGDAT_DEFINE
  43. #include    "msgdat.h"
  44.  
  45. static    jmp_buf        JmpBuf;
  46.  
  47. static    DLG_T        *Dlg = NULL;
  48. static    CNS_T        *Cns = NULL;
  49. static    EVT_T        *Evt = NULL;
  50. static    SBAR_T        *SbV = NULL;
  51.  
  52. #define                CNS_XS        (80)
  53. #define                CNS_YS        (24)
  54. #define                DLG_XS        (   4 + CNS_XS* 6 + 2+16+4)
  55. #define                DLG_YS        (20+4 + CNS_YS*12 + 4)
  56.  
  57. int        RsPort   = 0;
  58. int        RsBaud   = -1;
  59. int        RsMd     = -1;
  60. int        CtrlX    = FALSE;
  61. int        QuickRet = FALSE;
  62.  
  63. #if    1
  64. static                int        Ch;
  65. static                UINT    Ec;
  66. #define                BREAK_CHECK()    {                        \
  67.     Ch = EVT_getkey( &Ec );                                    \
  68.     if ( Ch == 3 || (Ec&0xFF14) == (KA_C|KA_BIT_CTRL) )        \
  69.         userbreak(1);                                        \
  70. }
  71. #else
  72. #define                BREAK_CHECK()
  73. #endif
  74.  
  75. static CONST char    RECODENAME_MAINARG[] = DTA_RECODENAME_MAINARG;
  76. static CONST char    RECODENAME_DSPCTRL[] = DTA_RECODENAME_DSPCTRL;
  77. DTACTRL_T            *DtaCtrl = NULL;
  78.  
  79. #define    MAIN_EVT_CANCEL    (9999)
  80.  
  81. static    PLT_RGB    PltDat[16] =
  82. {
  83.     /* G R  B                                */
  84.     { 0, 0, 0},    /* C_BALCK                    */
  85.     { 0, 0, 0},    /* C_BLUE or C_THROUGH        */
  86.     { 7, 7, 8},    /* C_RED                    */
  87.     {12,12,13},    /* C_MAGENTA                */
  88.     { 6, 5, 7},    /* C_GREEN                    */
  89.     { 8, 7, 9},    /* C_CYAN                    */
  90.     {10, 9,11},    /* C_YELLOW                    */
  91.     {11,11,12},    /* C_WHITE                    */
  92.  
  93.     { 5, 5, 6},    /* C_HBALCK                    */
  94.     { 0, 0,11},    /* C_HBLUE                    */
  95.     { 0,11, 0},    /* C_HRED                    */
  96.     { 0,11,11},    /* C_HMAGENTA                */
  97.     {11, 0, 0},    /* C_HGEEN                    */
  98.     {11, 0,11},    /* C_HCYAN                    */
  99.     {11,11, 0},    /* C_HYELLOW                */
  100.     {14,14,15}    /* C_HWHITE                    */
  101. };
  102.  
  103. void    userbreak(int level)
  104. {
  105.     extern    void    signal_func(int level);
  106.     signal_func( level );
  107. }
  108.  
  109. static int    ExitLevel = 0;
  110.  
  111. void    userexit(int level)
  112. {
  113.     ExitLevel = level;
  114.     longjmp(JmpBuf, 1);
  115. }
  116.  
  117. void    USR_dspFlush(void)
  118. {
  119.     SbV->top = Cns->dspPos.lnD;
  120.     SbV->dsp = Cns->dspYm;
  121.     SbV->min = 0;
  122.     SbV->max = Cns->linMaxD;
  123.     SBAR_Vredraw(SbV);
  124. }
  125.  
  126. int        USR_putc( int ch )
  127. {
  128.     BREAK_CHECK();
  129.  
  130.     ch &= 0xFF;
  131.     if ( ch == '\n' )
  132.     {
  133.         CNS_puts( Cns,"\r\n");
  134.         USR_dspFlush();
  135.     } else
  136.         CNS_putc( Cns, ch );
  137.     return (ch);
  138. }
  139.  
  140. int        USR_fputc( int ch, FILE *fp )
  141. {
  142.     ch &= 0xFF;
  143.  
  144.     if ( fp )
  145.     {
  146.         if ( fp == stdout || fp == stderr )
  147.             return USR_putc( ch );
  148.         else
  149.             return fputc( ch, fp );
  150.     }
  151. }
  152.  
  153. void    USR_puts( CONST char *s )
  154. {
  155.     BREAK_CHECK();
  156.  
  157.     while ( *s )
  158.     {
  159.         if ( *s == '\n' )
  160.         {
  161.             CNS_puts( Cns, "\r\n" );
  162.         } else
  163.         {
  164.             CNS_putc( Cns, *s );
  165.         }
  166.         ++s;
  167.     }
  168.     USR_dspFlush();
  169. }
  170.  
  171. void    USR_fputs( CONST char *s, FILE *fp )
  172. {
  173.     if ( fp )
  174.     {
  175.         if ( fp == stdout || fp == stderr )
  176.             USR_puts( s );
  177.         else
  178.             fputs( s, fp );
  179.     }
  180. }
  181.  
  182. void    USR_printf( CONST char *form, ... )
  183. {
  184.     va_list        arg;
  185.     char        tmp[BUFSIZ];
  186.  
  187.     va_start( arg, form );
  188.     vsprintf( tmp, form, arg );
  189.     va_end(arg);
  190.  
  191.     USR_puts( tmp );
  192. }
  193.  
  194. void    USR_fprintf( FILE *fp, CONST char *form, ... )
  195. {
  196.     va_list        arg;
  197.     char        tmp[BUFSIZ];
  198.  
  199.     va_start( arg, form );
  200.     vsprintf( tmp, form, arg );
  201.     va_end();
  202.  
  203.     if ( fp )
  204.     {
  205.         if ( fp == stdout || fp == stderr )
  206.             USR_puts( tmp );
  207.         else
  208.             fputs( tmp, fp );
  209.     }
  210. }
  211.  
  212. int        USR_getch(void)
  213. {
  214.     int        ch;
  215.     UINT    ec;
  216.  
  217.     if ( (ch = EVT_getkey( &ec )) == 0xFFFF )
  218.         return (0);
  219.     else
  220.     {
  221.         if ( ch == 3 || (ec&0xFF14) == (KA_C|KA_BIT_CTRL) )
  222.             userbreak(0);
  223.         return (ch & 0xFF);
  224.     }
  225. }
  226.  
  227. static    void    dspFunc( DLG_T *dlg )
  228. {
  229.     int        evtPos = 0;
  230.     int        x1 = dlg->fr.x0;
  231.     int        y1 = dlg->fr.y0;
  232.     int        x2 = x1 + dlg->fr.xs - 1;
  233. //    int        y2 = y1 + dlg->fr.ys - 1;
  234.     int        cx1, cy1, cx2, cy2;
  235.  
  236.     boxfHol( x1+2, y1+2, x2-2, y1+2+18, PSET, C_WHITE, C_HWHITE, C_DARK );
  237.     DSP_str( x1+4, y1+4, C_MBLACK, C_WHITE, 16,
  238.         "%s file(s) with ZMODEM/YMODEM/XMODEM Protocol",
  239. #ifdef    _SZ
  240.         "Send",
  241. #else
  242.         "Receive",
  243. #endif
  244.         MAIN_VER );
  245.  
  246.     /*    [EXIT]ボタン    --------------------------------------------*/
  247.     {
  248.         CONST int    x = x2 - 18;
  249.         CONST int    y = y1 + 3;
  250.         EGB_writeMode( EgbPtr, OPAQUE );
  251.         EGB_paintMode( EgbPtr, 0x022);
  252.         EGB_color( EgbPtr, 0, C_DARK );
  253.         EGB_color( EgbPtr, 1, C_WHITE );
  254.         _EGB_PUTBLOCKCOLOR(0, x, y, x+15, y+15, getds(), _icn16x16_closeBtn );
  255.         boxHol( x, y, x+15, y+15, PSET, C_HWHITE, C_DARK );
  256.         _EVT_SETMOS( Evt, evtPos++, MAIN_EVT_CANCEL, MOSEVT_BTN, BTN_LEFT,
  257.             x, y, x+15, y+15, 0, NULL );
  258.    }
  259.  
  260.     /*    ダイアログ移動用    ----------------------------------------*/
  261.     _EVT_SETMOS( Evt, evtPos++, 0, MOSEVT_NOACT|MOSEVT_QUICK, BTN_LEFT,
  262.         x1+2, y1+2, x2-2, y1+2+18, DLG_defMoveFunc, dlg );
  263.  
  264.     /* コンソール領域    */
  265.     cx1 = x1+4;
  266.     cy1 = y1+24;
  267.     cx2 = x1 + 4 + CNS_XS* 6 - 1;
  268.     cy2 = y1 +24 + CNS_YS*12 - 1;
  269.  
  270.     box( cx1-1, cy1-1, cx2+1, cy2+1, PSET, C_WHITE );
  271.     box( cx1-2, cy1-2, cx2+2, cy2+2, PSET, C_DARK  );
  272.     CNS_changeScnSize( Cns, cx1, cy1, cx2, cy2 );
  273.     CNS_flush( Cns, ERR, ERR);
  274.  
  275.     /* スクロールバー    */
  276.     SBAR_Vdsp( SbV, cx2+5, cy1, 16, cy2-cy1+1 );
  277.     USR_dspFlush();
  278. }
  279.  
  280. static int        funcSbarV( int evtNo, SBAR_T *sbV )
  281. {
  282.     CNS_T    *cns = Cns;
  283.  
  284.     if ( cns->linMaxD < cns->dspYm )
  285.     {
  286.         sbV->top = 0;
  287.         return (0);
  288.     } else if ( sbV->top > cns->homeY )
  289.         sbV->top = cns->homeY;
  290.  
  291.     switch( evtNo )
  292.     {
  293.         case    SBEVT_SLIDE:
  294.             MOS_DEC();
  295.             CNS_jump( cns, sbV->top );
  296.             MOS_INC();
  297.             break;
  298.         case    SBEVT_INC:
  299.             if ( sbV->top <= cns->homeY )
  300.             {
  301.                 MOS_DEC();
  302.                 CNS_nextLine( cns, 1 );
  303.                 MOS_INC();
  304.             }
  305.             break;
  306.         case    SBEVT_DEC:
  307.             MOS_DEC();
  308.             CNS_prevLine( cns, 1 );
  309.             MOS_INC();
  310.             break;
  311.     }
  312.     return (0);
  313. }
  314.  
  315.  
  316. static    int        dsp_init(void)
  317. {
  318.     /* イベント用    */
  319.     Evt = EVT_alloc(NULL,8);
  320.     SbV = SBAR_Vopen(Evt, funcSbarV, NULL );
  321.     Cns = CNS_open();
  322.  
  323.     /* ダイアログオープン    */
  324.     Dlg = DLG_open( DLGPOS_CENTER_OF_SCN, DLGPOS_CENTER_OF_SCN,
  325.         DLG_XS, DLG_YS, C_DLGBASE, dspFunc, NULL );
  326.     if ( Evt == NULL || SbV == NULL || Dlg == NULL || Cns == NULL )
  327.         return (ERR);
  328.     Cns->page = DspCtrl.writePage;
  329.     SbV->attr |= (SBATT_SLIDEQUICK | SBATT_MAXADJUST);
  330.     DLG_dsp(Dlg);
  331.  
  332.     return (NORMAL);
  333. }
  334.  
  335. void    dsp_term(void)
  336. {
  337.     if ( Cns )
  338.     {
  339.         CNS_close( Cns );
  340.         Cns = NULL;
  341.     }
  342.     if ( Dlg )
  343.     {
  344.         DLG_close( Dlg );
  345.         Dlg = NULL;
  346.     }
  347. }
  348.  
  349.  
  350. static    void    hit_any_key(void)
  351. {
  352.     int        ch;
  353.     UINT    ec;
  354.  
  355.     USR_puts("\n**** Hit any key *****\n");
  356.     MOS_curType(MOSCUR_NORMAL);
  357.     MOS_CON();
  358.  
  359.     LOOP
  360.     {
  361.         int        ret;
  362.  
  363.         USR_dspFlush();
  364.  
  365.         if ( (ret = EVT_chk(Evt,EVTIGN_KEY)) >= 0 )
  366.         {
  367.             if ( ret == MAIN_EVT_CANCEL )
  368.                 break;
  369.         } else
  370.         {
  371.             ch = EVT_getkey(&ec);
  372.             if ( ch != 0xFFFF || (ec & 0xFF00) != 0xFF00 )
  373.             {    ec &= 0xFF14;
  374.                 switch( ec )
  375.                 {
  376.                     case    KA_UP:
  377.                     case    KA_W|KA_BIT_CTRL:
  378.                     case    KA_PREV:
  379.                         MOS_DEC();
  380.                         CNS_prevLine( Cns, 1 );
  381.                         MOS_INC();
  382.                         break;
  383.                     case    KA_DOWN:
  384.                     case    KA_Z|KA_BIT_CTRL:
  385.                     case    KA_NEXT:
  386.                         MOS_DEC();
  387.                         CNS_nextLine( Cns, 1 );
  388.                         MOS_INC();
  389.                         break;
  390.                     case    KA_UP|KA_BIT_SHIFT:
  391.                     case    KA_R|KA_BIT_CTRL:
  392.                         MOS_DEC();
  393.                         CNS_prevLine( Cns, Cns->dspYm );
  394.                         MOS_INC();
  395.                         break;
  396.                     case    KA_DOWN|KA_BIT_SHIFT:
  397.                     case    KA_C|KA_BIT_CTRL:
  398.                         MOS_DEC();
  399.                         CNS_nextLine( Cns, Cns->dspYm );
  400.                         MOS_INC();
  401.                         break;
  402.                     default:
  403.                         goto    LOOP_EXIT;
  404.                 }
  405.             } else
  406.                 EVT_idl();
  407.         }
  408.     }
  409. LOOP_EXIT:
  410.     return;
  411. }
  412.  
  413. /*************************************************************************
  414. *    システム初期化
  415. *************************************************************************/
  416. static    int        init( int *argc, char **argv[], int _argc, char **_argv )
  417. {
  418.     DSPCTRL        *dsp = NULL;
  419.     DTARECODE_T    *dsp_recode = NULL;
  420.     DTARECODE_T    *arg_recode = NULL;
  421.  
  422. #ifdef    __HIGHC__
  423.     /* メモリ取得用設定    */
  424.     Init_allocated_storage = 0;
  425. #endif
  426.  
  427.     *argc = _argc;
  428.     *argv = _argv;
  429.  
  430.     if ( DTA_isAtPcl() )
  431.     {
  432.         /* 子プロセス起動    */
  433.         CONST char        *args = PROG_EXP;
  434.  
  435.         if ( (DtaCtrl = DTA_openCheck()) != NULL )
  436.         {
  437.             if ( (dsp_recode = DTA_openRecode( DtaCtrl, RECODENAME_DSPCTRL)) != NULL )
  438.             {    /* 表示システムの情報あり    */
  439.                 dsp = dsp_recode->ptr;
  440.             }
  441.             if ( (arg_recode = DTA_openRecode( DtaCtrl, RECODENAME_MAINARG)) != NULL )
  442.             {    /* パラメータの指定あり    */
  443.                 args = arg_recode->ptr;
  444.             }
  445.         }
  446.         if ( (*argc = FS_argSet( args, argv )) < 1 )
  447.             return (ERR);
  448.     }
  449.  
  450.     set_ctrl_c();
  451.     set24h();
  452.  
  453.     /* 表示システム初期化    */
  454.     if ( DSP_pcl_sysInit( dsp ) )
  455.         return    (ERR);
  456.  
  457.     MosInf.scrollPage = 0xFF;        /* スクロールなし    */
  458.  
  459.     DSP_set_defFnt( FNTYPE_USR1 );
  460.     GetFnt12Ptr(0);                /* ank font 12 初期設定        */
  461.     KBF_sysInit();                /* キーボードシステム初期化    */
  462.     KBF_kan_off();                /* かな漢字変換禁止            */
  463.     RS_init();
  464.     sdk_control(0);                /* サイドワーク起動禁止        */
  465.  
  466.     /* パレット設定    */
  467.     if ( dsp == NULL )
  468.         DSP_setPlt(PltDat);
  469.  
  470. #if    0
  471.     /* 起動ディレクトリの設定    */
  472.     if ( FS_setStartPath( StartPath, *argv[0] ) )
  473.         return (ERR);
  474.  
  475.     /* CCI システム初期化    */
  476.     if ( CCI_init( CodeSize, HeepSize) )
  477.         return (ERR);
  478. #endif
  479.  
  480.     return (NORMAL);
  481. }
  482.  
  483. static    void    term(int errLevel)
  484. {
  485.     if ( DtaCtrl )
  486.     {
  487.         if ( DtaCtrl->head )
  488.             DtaCtrl->head->ret = errLevel;
  489.         DTA_flush( DtaCtrl );
  490.         DTA_freeCtrl( DtaCtrl );
  491.         DtaCtrl = NULL;
  492.     }
  493.  
  494.     RS_end();
  495.     KBF_sysEnd();
  496.     DSP_sysEnd();
  497.  
  498.     reset24h();
  499. }
  500.  
  501. static    int        is_rsspeed( int spd )
  502. {
  503.     return ( spd ==  300 || spd ==  600 || spd ==  1200 || spd == 2400
  504.       || spd == 4800 || spd == 9600 || spd == 19200 );
  505. }
  506. static    int        get_baud(int spd)
  507. {
  508.     switch( spd )
  509.     {
  510.         case   300:    return RSBAUD_300;
  511.         case   600:    return RSBAUD_600;
  512.         case  1200:    return RSBAUD_1200;
  513.         case  2400:    return RSBAUD_2400;
  514.         case  4800:    return RSBAUD_4800;
  515.         case  9600:    return RSBAUD_9600;
  516.         case 19200:    return RSBAUD_19200;
  517.         default:    return (-1);
  518.     }
  519.  
  520. }
  521.  
  522. /*************************************************************************
  523. *    オプションパラメータの解析
  524. *************************************************************************/
  525. static    int        dec_opt( int *argc, char **argv[], int _argc, char **_argv )
  526. {
  527.     int        i, pos;
  528.     char    **av;
  529.  
  530.     if ( _argc < 1 )
  531.         return (ERR);
  532.  
  533.     if ( (av = calloc(sizeof(char *),_argc)) == NULL )
  534.         return (ERR);
  535.     *argv = av;
  536.     av[0] = _argv[0];
  537.     *argc = 1;
  538.     pos   = 1;
  539.     for ( i = 1; i < _argc; ++i )
  540.     {
  541.         char    *s;
  542.  
  543.         s = _argv[i];
  544.         if ( *s != '$' )
  545.         {
  546.             av[pos++] = s;
  547.             ++(*argc);
  548.             continue;
  549.         }
  550.         /* オプションデコード    */
  551.         ++s;
  552.         if ( *s != '-' )
  553.         {    /* プログラム名    */
  554.             av[0] = s;
  555.         } else
  556.         {
  557.             ++s;    /* skip '-' */
  558.             switch ( toupper(*s) )
  559.             {
  560.                 case 'Q':
  561.                 {
  562.                     QuickRet = TRUE;
  563.                     break;
  564.                 }
  565.                 case 'E':    /* 実行スピード        */
  566.                 {
  567.                     int                    speed;
  568.                     extern    unsigned    Effbaud;
  569.                     ++s;
  570.                     speed = atoi(s);
  571.                     if ( is_rsspeed(speed) )
  572.                         Effbaud =~ speed;
  573.                     break;
  574.                 }
  575.                 case 'B':    /* Baud(シリアルスピード)    */
  576.                 {
  577.                     ++s;
  578.                     RsBaud = get_baud( atoi(s) );
  579.                     break;
  580.                 }
  581.                 case 'M':    /* 通信モード    */
  582.                 {
  583.                     int        md;
  584.                     md = 0;
  585.                     ++s;
  586.                     while ( *s )
  587.                     {
  588.                         switch ( toupper(*s) )
  589.                         {
  590.                             case '7':    md &= (~RSMD_BIT_8);
  591.                             case '8':    md |=  RSMD_BIT_8;
  592.                             case 'N':    md &= ~(RSMD_PARITY);    break;
  593.                             case 'O':    md |= (RSMD_PARITY);    break;
  594.                             case 'E':    md |= (RSMD_PARITY|RSMD_PARITY_EVEN);    break;
  595.                             case '1':    md &= (~RSMD_STOPBIT_2);
  596.                             case '2':    md |=   RSMD_STOPBIT_2;
  597.                         }
  598.                         ++s;
  599.                     }
  600.                     RsMd = md;
  601.                     break;
  602.                 }
  603.             }
  604.         }
  605.     }
  606.     return (NORMAL);
  607. }
  608.  
  609. void    main(int _argc, char *_argv[])
  610. {
  611.     int            ret;
  612.     int         argc;
  613.     char        **argv;
  614.  
  615.     if ( init(&argc,&argv,_argc,_argv) )
  616.     {
  617.         ret = 1;
  618.     } else
  619.     {
  620.         if ( dsp_init() )
  621.         {
  622.             ret = 1;
  623.         } else
  624.         {
  625.             int        i;
  626.  
  627.             if ( (i = setjmp(JmpBuf)) > 0 )
  628.             {
  629.                 ret = i;
  630.                 goto END_EXIT;
  631.             }
  632.             {
  633.                 int         ac;
  634.                 char        **av;
  635.             
  636.                 if ( dec_opt(&ac,&av,argc,argv) == NORMAL )
  637.                     zm_main(ac,av);
  638.                 ret = 0;
  639.             }
  640. END_EXIT:
  641.             if ( !QuickRet )
  642.                 hit_any_key();
  643.             dsp_term();
  644.         }
  645.         term(ret);
  646.     }
  647.     FM_all_fclose();
  648.     pcl_exit(0);
  649.     exit(ret);
  650. }
  651.